import React, { Component } from 'react';
import ApproveHistory from 'components/template/approve-history';
import baseService from 'share/base.service';
import paymentService from '../payment-service';
import { Spin } from 'antd';

class PaymentHistory extends Component {

  componentDidMount() {
    this.init();
  }

  state = {
    approvalChains: [],
    approvalHistoryDTOs: [],
    applicant: paymentService.applicant,
    info: paymentService.paymentDetails
  };

  init = () => {
    this.setState({ loading: true });
    Promise.all([this.getInfo()])
      .finally(() => {
        this.setState({ loading: false });
      });
  };

  getApplicant = async () => {
    const { applicant } = this.state;
    const { info: { applicantOID } } = this.state;
    if (applicant.hasOwnProperty('id')) {
      return;
    }
    const ret = await baseService.getUserByOID(applicantOID);
    this.setState({ applicant: ret.data });
  };

  getInfo = async () => {
    const { paymentRequestOID } = this.props;
    const { info } = this.state;
    if (info.hasOwnProperty('paymentRequestOID')) {
      return await this.getApplicant();
    }
    const ret = await paymentService.getPaymentRequest(paymentRequestOID);
    await this.setState({ info: ret.data });
    paymentService.paymentDetails = ret.data;
    await this.getApplicant();
  };


  render() {
    const { approvalChains, approvalHistoryDTOs } = this.props;
    const {  applicant, loading, info } = this.state;
    return (
      <Spin spinning={loading}>
        <ApproveHistory
          businessCode={info.businessCode}
          approvalChains={approvalChains}
          approvalHistory={approvalHistoryDTOs}
          applicantInfo={applicant}
        />
      </Spin>
    );
  }
}

PaymentHistory.propTypes = {};

export default PaymentHistory;
